Skip to content

Method: buildAxiomValuesFromInstance(Object, AxiomValueGatherer)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.oom;
19:
20: import cz.cvut.kbss.jopa.model.MultilingualString;
21: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
22: import cz.cvut.kbss.jopa.model.metamodel.AbstractPluralAttribute;
23: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
24: import cz.cvut.kbss.jopa.oom.converter.ToLexicalFormConverter;
25: import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
26: import cz.cvut.kbss.ontodriver.model.Assertion;
27: import cz.cvut.kbss.ontodriver.model.Axiom;
28: import cz.cvut.kbss.ontodriver.model.NamedResource;
29: import cz.cvut.kbss.ontodriver.model.Value;
30:
31: import java.util.Collection;
32: import java.util.Collections;
33: import java.util.Objects;
34: import java.util.Set;
35: import java.util.function.Function;
36: import java.util.stream.Collectors;
37:
38: class PluralAnnotationPropertyStrategy<X> extends PluralDataPropertyStrategy<X> {
39:
40: PluralAnnotationPropertyStrategy(EntityType<X> et, AbstractPluralAttribute<? super X, ?, ?> att,
41: Descriptor attributeDescriptor, EntityMappingHelper mapper) {
42: super(et, att, attributeDescriptor, mapper);
43: }
44:
45: @Override
46: void addAxiomValue(Axiom<?> ax) {
47: final Object value = ax.getValue().getValue();
48: if (isValidRange(value)) {
49: values.add(toAttributeValue(value));
50: } else if (value instanceof NamedResource && IdentifierTransformer.isValidIdentifierType(elementType)) {
51: values.add(IdentifierTransformer
52: .transformToIdentifier(ToLexicalFormConverter.INSTANCE.convertToAttribute(value),
53: elementType));
54: }
55: }
56:
57: @Override
58: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) {
59: final Object value = extractFieldValueFromInstance(instance);
60:• assert value instanceof Collection || value == null;
61: final Collection<?> valueCollection = (Collection<?>) value;
62:• if (valueCollection == null || valueCollection.isEmpty()) {
63: valueBuilder.addValue(createAssertion(), Value.nullValue(), getAttributeWriteContext());
64: } else {
65: final Function<Object, Collection<Value<?>>> mapper = resolveValueMapper();
66: final Set<Value<?>> assertionValues =
67: valueCollection.stream().filter(Objects::nonNull).map(mapper).flatMap(Collection::stream)
68: .collect(Collectors.toSet());
69: valueBuilder.addValues(createAssertion(),
70: filterOutInferredValues(valueBuilder.getSubjectIdentifier(), assertionValues),
71: getAttributeWriteContext());
72: }
73: }
74:
75: private Function<Object, Collection<Value<?>>> resolveValueMapper() {
76: if (SingularAnnotationPropertyStrategy.isResourceIdentifierType(elementType)) {
77: return v -> Collections.singleton(new Value<>(NamedResource.create(IdentifierTransformer.valueAsUri(v))));
78: } else {
79: return v -> v instanceof MultilingualString ?
80: SingularMultilingualStringFieldStrategy.translationsToLangStrings(
81: (MultilingualString) v).collect(Collectors.toList()) : Collections.singleton(convertToAxiomValue(v));
82: }
83: }
84:
85: @Override
86: Collection<Value<?>> toAxiomValue(Object value) {
87: return resolveValueMapper().apply(value);
88: }
89:
90: @Override
91: Assertion createAssertion() {
92: return Assertion
93: .createAnnotationPropertyAssertion(attribute.getIRI().toURI(), getLanguage(), attribute.isInferred());
94: }
95: }